package com.approval.approvalapp.center.tz;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.JsdxListBean;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Jsdx1Activity extends BaseActivity implements Jxdx1Adapter.ClickListener, Jxdx1SelectAdapter.DelClickListener {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.upv)
    View upv;
    @Bind(R.id.yx)
    TextView yx;
    @Bind(R.id.bm_num)
    TextView bmNum;
    @Bind(R.id.agree_tv)
    LinearLayout agreeTv;
    @Bind(R.id.bottom_rl)
    RelativeLayout bottomRl;
    @Bind(R.id.bm_num1)
    TextView bm_num1;
    @Bind(R.id.blue_down)
    ImageView blue_down;
    @Bind(R.id.rv2)
    RecyclerView rv2;
    @Bind(R.id.root)
    LinearLayout root;
    @Bind(R.id.gray_view)
    View gray_view;
    private ProgressDialog progressDialog;
    private List<JsdxListBean> jsdxListBeans;
    private Jxdx1Adapter adapter;
    private List<JsdxListBean> selectlist;
    private List<JsdxListBean> blistBeanList;
    private Jxdx1SelectAdapter jxdx1SelectAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_jsdx1;
    }

    @Override
    public void initView() {
        selectlist = (List<JsdxListBean>) getIntent().getSerializableExtra("selectlist");

        List<String> selenum = new ArrayList<>();
        for (int i = 0; i < selectlist.size(); i++) {
            if (selectlist.get(i).isSelect()) {
                selenum.add("1");
            }
        }
        bmNum.setText(selenum.size() + "部门");

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在加载");
        progressDialog.setCancelable(false);
        progressDialog.show();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("areacode", SpUtil.getString(this, SpUtil.REGION_CODE, ""));
        jsonObject.put("is_select", "1");
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/deliver_an_announcement",
                jsonObject.toString(), new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        jsdxListBeans = JSON.parseArray(str, JsdxListBean.class);

                        for (int i = 0; i < selectlist.size(); i++) {
                            if (selectlist.get(i).isSelect()) {
                                jsdxListBeans.get(i).setSelect(true);
                            }
                        }

                        rv.setLayoutManager(new LinearLayoutManager(Jsdx1Activity.this));
                        adapter = new Jxdx1Adapter();
                        adapter.setLists(jsdxListBeans, Jsdx1Activity.this);
                        rv.setAdapter(adapter);
                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                        }
                    }

                    @Override
                    public void finish() {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                        }
                    }
                });

    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.back_iv, R.id.agree_tv, R.id.bottom_rl, R.id.blue_down})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.agree_tv:
                Intent intent = new Intent();
                //把返回数据存入Intent
                intent.putExtra("selectlist", (Serializable) jsdxListBeans);
                //设置返回数据
                this.setResult(RESULT_OK, intent);
                //关闭Activity
                finish();
                break;
            case R.id.bottom_rl:
                gray_view.setVisibility(View.VISIBLE);
                root.setVisibility(View.VISIBLE);
                rv2.setLayoutManager(new LinearLayoutManager(this));
                jxdx1SelectAdapter = new Jxdx1SelectAdapter();
                rv2.setAdapter(jxdx1SelectAdapter);
                blistBeanList = new ArrayList<>();
                for (int i = 0; i < jsdxListBeans.size(); i++) {
                    if (jsdxListBeans.get(i).isSelect()) {
                        blistBeanList.add(jsdxListBeans.get(i));
                    }
                }
                bm_num1.setText(blistBeanList.size() + "部门");
                jxdx1SelectAdapter.setLists(blistBeanList, Jsdx1Activity.this);
                break;
            case R.id.blue_down:
                gray_view.setVisibility(View.GONE);
                root.setVisibility(View.GONE);
                break;

        }
    }

    @Override
    public void click(int position) {
        if (jsdxListBeans.get(position).isSelect()) {
            jsdxListBeans.get(position).setSelect(false);
            adapter.notifyDataSetChanged();
        } else {
            jsdxListBeans.get(position).setSelect(true);
            adapter.notifyDataSetChanged();
        }

        List<String> selenum = new ArrayList<>();
        for (int i = 0; i < jsdxListBeans.size(); i++) {
            if (jsdxListBeans.get(i).isSelect()) {
                selenum.add("1");
            }
        }

        bmNum.setText(selenum.size() + "部门");
    }

    @Override
    public void del(int position) {
        for (int i = 0; i <jsdxListBeans.size() ; i++) {
            if (jsdxListBeans.get(i).getOrgcode().equals(blistBeanList.get(position).getOrgcode())){
                jsdxListBeans.get(i).setSelect(false);
            }
        }



        adapter.notifyDataSetChanged();
        blistBeanList.remove(position);
        jxdx1SelectAdapter.notifyDataSetChanged();
        bm_num1.setText(blistBeanList.size() + "部门");
        bmNum.setText(blistBeanList.size() + "部门");
    }
}
